package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 新增跟进记录表
     *
     * @param clueTrackRecordVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult add(ClueTrackRecordVo clueTrackRecordVo) {
        //1.查询线索表中用户基本信息
        TbClue tbClue = tbClueMapper.selectTbClueById(clueTrackRecordVo.getClueId());
        if (ObjectUtils.isEmpty(tbClue)) {
            //如果该线索不存在，那么添加线索表并添加跟进信息
            tbClue = new TbClue();
            tbClue.setStatus("2");//状态改为待跟进
            tbClue.setId(clueTrackRecordVo.getClueId());
            tbClue.setName(clueTrackRecordVo.getName());
            tbClue.setSex(clueTrackRecordVo.getSex());
            tbClue.setAge(clueTrackRecordVo.getAge());
            tbClue.setQq(clueTrackRecordVo.getQq());
            tbClue.setWeixin(clueTrackRecordVo.getWeixin());
            tbClueMapper.updateTbClue(tbClue);
        }
        //2.添加跟进信息
        //添加线索id
        clueTrackRecordVo.setClueId(tbClue.getId());
        clueTrackRecordVo.setCreateTime(DateUtils.getNowDate());
        clueTrackRecordVo.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecordMapper.add(clueTrackRecordVo);
        return AjaxResult.success();
    }

    /**
     * 查询线索跟进
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        TbClue tbClue = tbClueMapper.selectTbClueById(clueId);
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.list(tbClue.getName());

        return list;
    }
}
